package com.justgame.project.service.art.service;

import com.justgame.project.cloud.common.exception.Exc;
import com.justgame.project.cloud.common.util.ObjUtil;
import com.justgame.project.service.api.dto.ArtPageCommentDTO;
import com.justgame.project.service.api.entity.ArtPageInfo;
import com.justgame.project.cloud.common.entity.PageRequest;
import com.justgame.project.service.api.vo.ArtPageCommentVO;
import com.justgame.project.service.api.vo.ArtPageInfoVO;
import com.justgame.project.service.art.mapper.ArtPageCommentMapper;
import com.justgame.project.service.art.mapper.ArtPageInfoMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Slf4j
public class ArtPageCommentService {
    private final ArtPageInfoMapper artPageInfoMapper;
    private final ArtPageCommentMapper pageCommentMapper;


    public PageRequest<ArtPageCommentVO> getList(PageRequest<ArtPageCommentVO> pageRequest, ArtPageCommentDTO dto) {
        if (StringUtils.isBlank(dto.getPageId())) {
            throw new Exc("请先指定要查询的文章ID！");
        }
        /* 查询评论数量 */
        Long commentCount = pageCommentMapper.getCommentCount(dto.getPageId());
        Map<String, ArtPageCommentVO> result = new LinkedHashMap<>();
        /* 如果评论量>0，再继续查相关评论，否则直接返回 */
        if (commentCount != 0) {
            /* 评论信息（含主评论及相关回复评论） */
            List<ArtPageCommentVO> comments = pageCommentMapper.getList(pageRequest, dto);
            for (ArtPageCommentVO comment : comments) {
                /* 是评论，不是回复 */
                if (comment.getParentId() == null) {
                    result.put(comment.getId(), comment);
                } else {
                    /* 是回复，不是评论 */
                    ArtPageCommentVO parent = result.get(comment.getParentId());
                    if (parent.getReplies() == null) {
                        parent.setReplies(new ArrayList<>());
                    }
                    parent.getReplies().add(comment);
                }
            }
        }
        return pageRequest.BuildPageInfo(new ArrayList<>(result.values()), commentCount);
    }

    public Boolean save(ArtPageCommentDTO dto) {
        if (StringUtils.isBlank(dto.getPageId())) {
            throw new Exc("未知的文章评论！");
        }
        ObjUtil.inject(dto, true);

        /* 是回复 */
        if (StringUtils.isNotBlank(dto.getParentId())) {
            ArtPageCommentVO parent = pageCommentMapper.selectById(dto.getParentId());
            /* 查看上级评论是否存在 */
            if (parent == null) {
                throw new Exc("暂无法回复，因无法找到上级评论");
            }
            /* 查看是否是统一文章ID */
            if (!parent.getPageId().equals(dto.getPageId())) {
                throw new Exc("文章信息与回复数据不一致，请重新再试！");
            }
            dto.setType(2);
            dto.setParentId(parent.getId());
            dto.setAncestors(String.format("%s,%s", parent.getAncestors(), dto.getId()));
            log.info("用户{}已于{}回复一条文章评论{}", dto.getCreateUser(), new Date(), parent.getId());
        } else {
            ArtPageInfo pageQuery = new ArtPageInfo();
            pageQuery.setId(dto.getPageId());
            List<ArtPageInfoVO> page = artPageInfoMapper.select(pageQuery);
            if (page == null || page.size() == 0) {
                throw new Exc("该文章不存在，无法评论！");
            }
            dto.setParentId(null);
            dto.setType(1);
            dto.setAncestors(dto.getId());
            log.info("用户{}已于{}发布一条文章评论", dto.getCreateUser(), new Date());
        }
        dto.setIsPixelate(dto.getIsPixelate() == null || dto.getIsPixelate() != 0 ? 1 : 0);
        dto.setIsRemove(0);
        return pageCommentMapper.Insert(dto) > 0;
    }

    public Boolean Delete(ArtPageCommentDTO dto) {
        if (dto.getIds() == null || dto.getIds().size() == 0) {
            throw new Exc("请先选择你要删除的数据！");
        }
        ObjUtil.inject(dto, false);
        return pageCommentMapper.Delete(dto) > 0;
    }

    public Boolean updateById(ArtPageCommentDTO dto) {
        if (!ObjUtil.checkId(dto)) {
            throw new Exc("请重新选择后再试！");
        }
        ObjUtil.inject(dto, false);
        return pageCommentMapper.UpdateById(dto) > 0;
    }

}
